package com.scuvc.controller;

import com.scuvc.controller.admin.AdminView;
import com.scuvc.controller.user.UserView;
import com.scuvc.dao.impl.*;
import com.scuvc.entity.PageBean;
import com.scuvc.service.impl.*;
import com.scuvc.utils.ImageUtils;
import com.scuvc.utils.StaticUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class View extends JFrame {
    static{
        StaticUtils.userInfoDao = new UserInfoDaoImpl();
        StaticUtils.userInfoService = new UserInfoServiceImpl();
        StaticUtils.carInfoDao = new CarInfoDaoImpl();
        StaticUtils.carInfoService = new CarInfoServiceImpl();
        StaticUtils.brandInfoDao = new BrandInfoDaoImpl();
        StaticUtils.brandInfoService = new BrandInfoServiceImpl();
        StaticUtils.modelInfoDao = new ModelInfoDaoImpl();
        StaticUtils.modelInfoService = new ModelInfoServiceImpl();
        StaticUtils.categoryInfoDao = new CategoryInfoDaoImpl();
        StaticUtils.categoryInfoService = new CategoryInfoServiceImpl();
        StaticUtils.recordInfoDao = new RecordInfoDaoImpl();
        StaticUtils.recordInfoService = new RecordInfoServiceImpl();

        StaticUtils.pageBean = new PageBean<>();
        StaticUtils.pageBean.setCurPage(1);
        StaticUtils.pageBean.setPrePage();
        StaticUtils.pageBean.setNextPage();
        StaticUtils.pageBean.setDataList(StaticUtils.carInfoService.getAll());
        StaticUtils.pageBean.setTotalPage();

        StaticUtils.carList = StaticUtils.carInfoService.getAllPage(StaticUtils.pageBean.getCurPage(),
                StaticUtils.pageBean.getPageSize());
    }
    public void start(){
        setTitle("城市租车系统");
        //设置窗体起始位置
        setLocation(400,100);
        //设置窗体大小
        setSize(645,400);
        //把图片添加到标签里
        ImageIcon img = new ImageIcon("src/images/bg_index.jpeg");
        ImageIcon bg = ImageUtils.createAlphaIcon(img,0.5f,
                img.getIconWidth(),img.getIconHeight());
        JLabel label = new JLabel(bg);
        //设置标签的大小
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        //把标签放在分层面板的最底层
        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //把窗口面板设为内容面板并设为透明，流动布局
        JPanel pan = (JPanel)getContentPane();
        pan.setOpaque(false);
        pan.setLayout(new BorderLayout());

        JLabel top = new JLabel();
        top.setText("登\u3000录");
        top.setFont(new Font("仿宋",Font.BOLD,36));
        top.setForeground(Color.BLUE);
        //设置标签居中
        top.setHorizontalAlignment(SwingConstants.CENTER);
        //设置该标签为北方
        pan.add(top,BorderLayout.NORTH);

        JPanel panCenter = new JPanel();
        panCenter.setOpaque(false);
        GridLayout gl = new GridLayout(5,3);
        gl.setHgap(10);
        gl.setVgap(20);
        panCenter.setLayout(gl);

        JLabel j1L1 = new JLabel();
        JLabel jName = new JLabel("用户名：");
        jName.setHorizontalAlignment(SwingConstants.CENTER);
        jName.setFont(new Font("仿宋",Font.BOLD,20));
        JTextField j1F4 = new JTextField();
        j1F4.setFont(new Font("仿宋",Font.BOLD,24));
        JLabel j1L5 = new JLabel();

        JLabel j2L1 = new JLabel();
        JLabel j2L2 = new JLabel();
        JLabel j2L4 = new JLabel();
        JLabel j2L5 = new JLabel();

        JLabel j3L1 = new JLabel();
        JLabel jPass = new JLabel("密\u3000码：");
        //设置对齐方式
        jPass.setHorizontalAlignment(SwingConstants.CENTER);
        jPass.setFont(new Font("仿宋",Font.BOLD,20));
        JPasswordField jpf4 = new JPasswordField();
        jpf4.setFont(new Font("仿宋",Font.BOLD,24));
        JLabel j3L5 = new JLabel();

        JLabel j5L1 = new JLabel();
        Font jFont = new Font("仿宋",Font.BOLD,20);
        JButton jbLog = new JButton("登\u3000录");
        jbLog.setFont(jFont);
        JButton jbReg4 = new JButton("注\u3000册");
        jbReg4.setFont(jFont);
        JLabel j5L5 = new JLabel();

        panCenter.add(j2L1);
        panCenter.add(j2L2);
        panCenter.add(j2L4);
        panCenter.add(j2L5);

        panCenter.add(j1L1);
        panCenter.add(jName);
        panCenter.add(j1F4);
        panCenter.add(j1L5);

        panCenter.add(j3L1);
        panCenter.add(jPass);
        panCenter.add(jpf4);
        panCenter.add(j3L5);

        panCenter.add(j5L1);
        panCenter.add(jbLog);
        panCenter.add(jbReg4);
        panCenter.add(j5L5);


        JPanel panDown = new JPanel();
        JLabel jLabel = new JLabel();
        panDown.setOpaque(false);
        panDown.add(jLabel);
        //输入框获取焦点、失去焦点监听事件
        FocusListener j1F4Listener = new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                //获取焦点时操作
                j1F4.setBackground(Color.BLACK);
                j1F4.setForeground(Color.WHITE);
            }
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时操作
                j1F4.setBackground(Color.WHITE);
                j1F4.setForeground(Color.BLACK);
            }
        };
        //输入框获取焦点、失去焦点监听事件
        FocusListener jpf4Listener = new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                //获取焦点时操作
                jpf4.setBackground(Color.BLACK);
                jpf4.setForeground(Color.WHITE);
            }
            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时操作
                jpf4.setBackground(Color.WHITE);
                jpf4.setForeground(Color.BLACK);
            }
        };
        //添加事件
        j1F4.addFocusListener(j1F4Listener);
        jpf4.addFocusListener(jpf4Listener);

        jbLog.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO 获取用户名和密码并进行判断
                //点击事件

                if(account.equals("")){
                    JOptionPane.showMessageDialog(null,
                            "账户不能为空","提示",
                            JOptionPane.INFORMATION_MESSAGE);
                }
                if(password.equals("")){
                    JOptionPane.showMessageDialog(null,
                            "密码不能为空","提示",
                            JOptionPane.INFORMATION_MESSAGE);
                }
                // TODO 登录成功后跳转的页面业务
            }
        });
        pan.add(panCenter,BorderLayout.CENTER);
        pan.add(panDown,BorderLayout.SOUTH);
        //设置窗体大小是否可调整
        setResizable(false);
        //设置窗体是否可见
        setVisible(true);
        //单击关闭按钮执行的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jbReg4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RegisterView rv = new RegisterView();
                setVisible(false);
                rv.register();
            }
        });
    }
}

